package com.yan.springbootstudy.common.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.yan.springbootstudy.common.datasource.DataOperationContext;
import com.yan.springbootstudy.common.datasource.DataOperationContextHolder;

/**
 * 
 * @Title: DataOperationFilter.java
 * @Package com.yan.springbootstudy.common.filter
 * @Description: 操作过滤器，写操作关键变量到线程
 * @author yan
 * @date 2017年8月30日
 * @version V1.0
 */
public class DataOperationFilter implements Filter {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());


	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		logger.debug("filterChangeDataOperation");
		DataOperationContext dataOperationContext = new DataOperationContext();
		HttpServletRequest httpRequest = (HttpServletRequest)request;
		//得到请求方法类型
		dataOperationContext.setOperation(httpRequest.getMethod());
		logger.debug("operation:" + dataOperationContext.getOperation());
		//得到关键业务属性
		//得到用户id
		dataOperationContext.setUserId(request.getParameter("userId"));
		logger.debug("userId:" + dataOperationContext.getUserId());
		DataOperationContextHolder.setDataOperationContext(dataOperationContext);
		chain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig config) throws ServletException {
		// TODO Auto-generated method stub

	}

}
